package com.module.osr.stormbringer.view;

import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;

import com.ahq.main.H1SelectionBorder;
import com.dalonedrau.jogl.engine.Action;
import com.dalonedrau.jogl.engine.GameApplet;
import com.dalonedrau.jogl.engine.GameState;
import com.dalonedrau.jogl.engine.InputEvent;
import com.dalonedrau.jogl.opengl.View;
import com.dalonedrau.jogl.opengl.sprite.BasicSpriteObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteBase;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObject;
import com.dalonedrau.jogl.opengl.sprite.SpriteImageObjectFactory;
import com.dalonedrau.jogl.opengl.ui.GuiComponent;
import com.dalonedrau.jogl.opengl.ui.Label;
import com.dalonedrau.jogl.opengl.ui.UIFactory;
import com.dalonedrau.jogl.opengl.ui.border.EmptyBorder;
import com.dalonedrau.jogl.opengl.ui.border.VXBorder;
import com.dalonedrau.jogl.opengl.ui.border.VXBorder2;
import com.dalonedrau.jogl.opengl.ui.menu.MenuGroup;
import com.dalonedrau.jogl.opengl.ui.menu.MenuItem;
import com.dalonedrau.jogl.opengl.ui.menu.VerticalMenu;
import com.dalonedrau.jogl.opengl.ui.panel.CardPanel;
import com.dalonedrau.jogl.opengl.ui.panel.LandscapePagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.PagePanel;
import com.dalonedrau.jogl.opengl.ui.panel.Panel;
import com.dalonedrau.jogl.opengl.ui.panel.PortraitPagePanel;
import com.dalonedrau.jogl.opengl.ui.view.LoadingView;
import com.dalonedrau.jogl.util.UtilityMethods;
import com.dalonedrau.pool.PooledStringBuilder;
import com.dalonedrau.pool.StringBuilderPool;
import com.module.osr.engine.OSRController;

/**
 * The title screen view.
 * @author Donald
 */
public final class IntroView implements View {
	/** the title label. */
	private Label		label;
	/** the title screen menu. */
	private MenuGroup	menu;
	/** the panel used to organize menu items. */
	private PagePanel	panel;
	/** flag indicating initialization has been completed. */
	private boolean		runInit	= false;
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleKeyEvent(final KeyEvent e) {
		switch (e.getKeyCode()) {
		case KeyEvent.VK_ESCAPE:
			System.out.println("esc");
			GameApplet.getInstance().exit();
			break;
		case KeyEvent.VK_UP:
			menu.highlightPrevious();
			break;
		case KeyEvent.VK_DOWN:
			menu.highlightNext();
			break;
		case KeyEvent.VK_ENTER:
			if (menu != null
					&& menu.isVisible()
					&& menu.getHighlighted() != null) {
				menu.getHighlighted().onSelect();
			}
			break;
		case KeyEvent.VK_TAB:
			if (menu != null) {
				if (e.isShiftDown()) {
					menu.highlightPrevious();
				} else {
					menu.highlightNext();
				}
			}
			break;
		case KeyEvent.VK_BACK_SPACE:
			try {} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		case KeyEvent.VK_SPACE:
			try {
				// textfield.addCharacter(e.getKeyChar());
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
			break;
		default:
			if (Character.isLetterOrDigit(e.getKeyChar())) {
				try {
					// textfield.addCharacter(e.getKeyChar());
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
			break;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMouseOver(final float x, final float y) {
		panel.handleMouseOver(x, y);
		switch (menu.getHighlightedIndex()) {
		case 0:
			pnlDesc.displayChild("ADD");
			break;
		case 1:
			pnlDesc.displayChild("DQ");
			break;
		case 2:
			pnlDesc.displayChild("YK");
			break;
		default:
			pnlDesc.displayChild("blank");
			break;
		}
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void handleMousePress(final MouseEvent e, final float x,
			final float y) {
		panel.handleMouseClick(e, x, y);
	}
	/**
	 * Initializes the {@link View}.
	 * @throws Exception if an error occurs
	 */
	private void init() throws Exception {
		initTitle();
		initMenu();
		this.initDescriptionPanel();
		panel = new PortraitPagePanel(Panel.PORTRAIT_CENTER);
		panel.addChild(label);
		Panel middle = new LandscapePagePanel(Panel.LANDSCAPE_CENTER);
		middle.addChild(menu);
		middle.addChild(this.pnlDesc);
		panel.addChild(middle);
		Point pt = UtilityMethods.getInstance().centerComponentInFrame(
				panel);
		panel.setPosition(pt.x, pt.y, 0.0f);
		panel.setVisible(true);
		pnlDesc.displayChild("YK");
		runInit = true;
	}
	/**
	 * Initializes the start menu.
	 * @throws Exception if an error occurs
	 */
	private void initMenu() throws Exception {
		menu = new VerticalMenu();
		menu.setBorder(new EmptyBorder(8, 8, 8, 8));
		MenuItem item1 = new MenuItem(
				"quentin_20",
				"The Nations of Flanaess"
				);
		item1.setAlignment(GuiComponent.CENTER, GuiComponent.CENTER);
		item1.setHighlightBorder(new H1SelectionBorder());
		item1.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					// load screen should switch to world map
					/*
					 * LoadingView.getInstance().setNextState(GameState.WORLD_MAP
					 * ); LoadingView.getInstance().setLoadAction(
					 * OverworldView.getInstance(),
					 * OverworldView.getInstance().getClass().getMethod(
					 * "loadMap", String.class, String.class), new
					 * String[]{"com/module/bp/dat/xml/bp_map_new.xml",
					 * "com/module/bp/dat/xml/hex_map.xml"} );
					 * LoadingView.getInstance().setRendered(false); // game
					 * state goes to load screen
					 * GameState.getInstance().setCurrentState
					 * (GameState.LOADING); //
					 * GameState.getInstance().setCurrentState(State.IN_PLAY);
					 */
					InputEvent.getInstance().addAction(
							OSRController.getInstance(), // object
							OSRController.class.getMethod("startGame", null), 
							null
							);
					//OSRController.getInstance().startGame();
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		menu.addItem(item1);
		MenuItem item2 = new MenuItem(
				"thalia_24",
				"The Frontiers of Alusia"
				);
		item2.setAlignment(GuiComponent.CENTER, GuiComponent.CENTER);
		item2.setHighlightBorder(new H1SelectionBorder());
		item2.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {
					InputEvent.getInstance().addAction(
							OSRController.getInstance(), // object
							OSRController.class.getMethod("startGame", null), 
							null
							);
					//OSRController.getInstance().startGame();
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		menu.addItem(item2);
		MenuItem item3 = new MenuItem(
				"stormbringer_21",
				"ThE YounG KingdomS"
				);
		item3.setAlignment(GuiComponent.CENTER, GuiComponent.CENTER);
		item3.setHighlightBorder(new H1SelectionBorder());
		item3.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				try {

					InputEvent.getInstance().addAction(
							OSRController.getInstance(), // object
							OSRController.class.getMethod(
									"loadStormbringerCharCreator", null), 
							null
							);
					/*
					// load screen should switch to STORMBINGER CHAR CREATOR
					 LoadingView.getInstance().setNextState(
							 GameState.STATE_13SB_CHAR_CREATOR); 
					 LoadingView.getInstance().setLoadAction(
							 SBCharCreator.getInstance(),
							 SBCharCreator.getInstance().getClass().getMethod(
									 "init"), new Object[]{});
					 LoadingView.getInstance().setRendered(false); 
					 // game state goes to load screen
					 GameState.getInstance().setCurrentState(
							 GameState.STATE_11LOADING);
					OSRController.getInstance().startGame();
					*/
				} catch (Exception ex) {
					JOGLErrorHandler.getInstance().fatalError(ex);
				}
			}
		});
		menu.addItem(item3);
		MenuItem item4 = UIFactory.getInstance().getStandardMenuItem(
				"EXIT");
		item4.addAction(new Action() {
			/**
			 * {@inheritDoc}
			 */
			@Override
			public void performAction() {
				GameApplet.getInstance().exit();
			}
		});
		menu.addItem(item4);
		menu.highlightIndex(0);
	}
	/**
	 * Initializes the title image.
	 * @throws Exception if an error occurs
	 */
	private void initTitle() throws Exception {
		SpriteBase sb = new BasicSpriteObject();
		SpriteImageObject img =
				SpriteImageObjectFactory.getInstance().getImageByName(
						"osr_title");
		sb.setImageRefId(img.getRefId());
		sb.setWidth(img.getWidth());
		sb.setHeight(img.getHeight());
		label = new Label(sb);
	}
	private CardPanel pnlDesc;
	private void initDescriptionPanel() {
		pnlDesc = new CardPanel();
		pnlDesc.setBorder(new VXBorder());
		PooledStringBuilder sb = 
			StringBuilderPool.getInstance().getStringBuilder();
		sb.append("AD&D");
		Label lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("ADD", lbl);
		
		sb.setLength(0);
		sb.append("DragonQuest");
		lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("DQ", lbl);
		
		sb.setLength(0);
		sb.append("With the destruction of Imrryr\n");
		sb.append("and the exile of the Melniboneans,\n");
		sb.append("economic activity moves south.\n");
		sb.append("From the lush coasts of Dharijor\n");
		sb.append("to the barren plains of the\n");
		sb.append("Weeping Waste, seek glory and\n");
		sb.append("adventure in the Young Kingdoms.");
		lbl = UIFactory.getInstance().getStandardH1Label(sb.toString());
		pnlDesc.addChild("YK", lbl);
		pnlDesc.addChild("blank", 
				UIFactory.getInstance().getStandardH1Label(""));
		sb.returnToPool();
		lbl = null;
	}
	/**
	 * {@inheritDoc}
	 */
	@Override
	public void prepareForRendering() throws Exception {
		if (!runInit) {
			try {
				init();
			} catch (Exception ex) {
				JOGLErrorHandler.getInstance().fatalError(ex);
			}
		}
		panel.prepareForRendering();
	}
}
